package com.ruoyi.back.service.impl

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.baomidou.shaun.core.context.ProfileHolder
import com.ruoyi.app.common.*
import com.ruoyi.back.domain.SystemNotice
import com.ruoyi.back.domain.TemplateReport
import com.ruoyi.back.domain.dto.CheckTemplateDto
import com.ruoyi.back.domain.dto.TemplateReportSearchDto
import com.ruoyi.back.domain.vo.ResolveStatusSumVo
import com.ruoyi.back.domain.vo.TemplateReportListVo
import com.ruoyi.back.enums.CheckStatusEnum
import com.ruoyi.back.mapper.TemplateReportMapper
import com.ruoyi.back.service.TemplateCategoryService
import com.ruoyi.back.service.TemplateReportService
import com.ruoyi.back.service.TemplateService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class TemplateReportServiceImpl : ServiceImpl<TemplateReportMapper, TemplateReport>(), TemplateReportService {

    @Autowired
    private lateinit var templateService: TemplateService

    @Autowired
    private lateinit var templateCategoryService: TemplateCategoryService

    override fun saveTemplateReport(templateReport: TemplateReport): Data<Boolean> =
        Ok("举报成功", save(templateReport.apply {
            createBy = ProfileHolder.getProfile().id.toLong()
        }))

    override fun templateReportPage(templateReportSearchDto: TemplateReportSearchDto): PageData<TemplateReportListVo> =
        TableSupport.buildPageRequest().let { pageDomain ->
            page(Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()),
                KtQueryWrapper(TemplateReport::class.java).apply {
                    eq(templateReportSearchDto.type != null, TemplateReport::type, templateReportSearchDto.type)
                    eq(templateReportSearchDto.status != null, TemplateReport::status, templateReportSearchDto.status)
                    gt(
                        StrUtil.isNotBlank(templateReportSearchDto.reportBeginDate),
                        SystemNotice::sendTime,
                        templateReportSearchDto.reportBeginDate.date()
                    )
                    lt(
                        StrUtil.isNotBlank(templateReportSearchDto.reportEndDate),
                        SystemNotice::sendTime,
                        templateReportSearchDto.reportEndDate.date()
                    )
                    orderByDesc(TemplateReport::createTime)
                }).let { page: Page<TemplateReport> ->
                Page<TemplateReportListVo>().apply {
                    BeanUtil.copyProperties(page, this)
                    records = page.records.takeIf { it.isNotEmpty() }?.run {
                        map {
                            TemplateReportListVo().apply {
                                BeanUtil.copyProperties(it, this)
                                typeDesc = it.type?.reportType()
                                pic = it.pics?.split(",")
                                templateService.getById(it.templateId)?.let {
                                    templateContent = it.content
                                    templateTitle = it.title
                                    templateType = it.type
                                    templateCover = it.cover
                                    categoryId = it.categoryId
                                    categoryName = templateCategoryService.getNameById(it.categoryId!!)
                                }
                            }
                        }
                    }
                }.pageInfo()
            }
        }

    override fun checkTemplateReport(checkTemplateDto: CheckTemplateDto): Data<Boolean> =
        getById(checkTemplateDto.id)?.let { templateReport ->
            updateById(templateReport.apply {
                status = 2
            }).takeIf { it }
                ?.run {
                    if (checkTemplateDto.checkStatus == CheckStatusEnum.CHECK_PASS.code) {
                        templateService.removeById(templateReport.templateId)
                    }
                    Ok("审核成功", true)
                } ?: Fail("更新失败")
        } ?: Fail("暂无资源")

    override fun resolveStatusSum(): Data<ResolveStatusSumVo> =
        Ok("获取处理状态统计成功", ResolveStatusSumVo().apply {
            preResolveNum = count(KtQueryWrapper(TemplateReport::class.java)
                .eq(TemplateReport::status, 1)).toLong()
            haveResolveNum = count(KtQueryWrapper(TemplateReport::class.java)
                .eq(TemplateReport::status, 2)).toLong()
        })

    override fun removeCheckTemplate(ids: String): Data<Boolean> =
        Ok("移除成功", removeByIds(ids.split(",")))
}